﻿/*
    @Author: HungDinh
*/
jQuery.validator.addMethod(
    "regex",
        function(value, element, regexp) {
            if (this.optional(element)) return true;
           
            var re = new RegExp(regexp);
            return re.test(value);
        },
    "This value doesn\'t match the acceptable pattern"
);

jQuery.validator.addMethod(
    "stringaccept",
        function(value, element, pattern) {
            if (this.optional(element)) return true;
            else return jQueryAS_HasOnlyCharacters(value, pattern);
        },
    "This value doesn\'t match the acceptable pattern"
);

jQuery.validator.addMethod(
    "stringunaccept",
        function(value, element, pattern) {
            if (this.optional(element)) return true;
            else return (!jQueryAS_HasInvalidCharacters(value, pattern));
        },
    "This value contains the unacceptable pattern"
);

jQuery.validator.addMethod(
    "greaterthan",
        function(value, element, param) {
                return this.optional(element) || value > param;
        },
    "Please enter a value greater than to {0}"
);

jQuery.validator.addMethod(
    "lessthan",
        function(value, element, param) {
            return this.optional(element) || value < param;
        },
    "Please enter a value less than to {0}"
);

jQuery.validator.addMethod(
    "listboxhasitem",
       jQueryAS_ListBoxHasItem,
    "Please add items to listbox"
);

jQuery.validator.addMethod(
    "radlistboxrequire",
        function(value, element, param) {
            return jQueryAS_RadListBoxSelected(param);
        },
    "Please choose a least one item"
);

jQuery.validator.addMethod(
    "radlistboxhasitem",
        function(value, element, param) {
            return RadjQueryAS_ListBoxHasItem(param);
        },
    "Please add items to listbox"
);
        jQuery.validator.addMethod(
    "RadComboValueRequired",
        function(value, element, param) {
            return RadjQueryAS_RadComboValueRequired(param);
        },
    "Please choose a least one item"
);

function jQueryAS_ListBoxHasItem(value, element) {
    if (element.options == null) return false;
    else if (element.options.length > 0) return true;
    else return false;
}
function jQueryAS_RadListBoxSelected(clientId) {
    var radListBox = $find(clientId);
    var selectedItems = radListBox.get_selectedItems();
    if (selectedItems == null) return false;
    else if (selectedItems.length > 0) return true;
    return false;
}
function RadjQueryAS_ListBoxHasItem(clientId) {
    var radListBox = $find(clientId);
    var items = radListBox.get_items();
    if (items == null) return false;
    else if (items.get_count() > 0) return true;
    return false;
}
function RadjQueryAS_RadComboValueRequired(clientId) {   
    var radcombo = $find(clientId);   
    if (radcombo != null) {        
        if (radcombo.get_value().length <= 0) return false;
    }
    return true;
}
function jQueryAS_HasInvalidCharacters(input, invalid_chars) {
    if (invalid_chars == null) return true;
    if (invalid_chars == '') return true;
    var invalidHexValue = '';
    for (var i = 0; i < invalid_chars.length; i++) {
        invalidHexValue += '\\x' + invalid_chars.charCodeAt(i).toString(16).toUpperCase();
    }
    return input.search(new RegExp('[' + invalidHexValue + ']', 'ig')) >= 0;
}
function jQueryAS_HasOnlyCharacters(input, valid_chars) {
    if (valid_chars == null) return true;
    if (valid_chars == '') return true;
    var validHexValue = '';
    for (var i = 0; i < valid_chars.length; i++) {
        validHexValue += '\\x' + valid_chars.charCodeAt(i).toString(16).toUpperCase();
    }
    var m = input.match(new RegExp('[' + validHexValue + ']', 'ig'));
    if (m != null) {
        return m.length == input.length;
    }
    return false;
}

function jQueryAS_ScrollToSummaryMessage(isValid, errorWrapperId) {
    var errorWrapper = document.getElementById(errorWrapperId);
    if (!isValid) {
        errorWrapper.style.display = "block";
        var targetOffset = errorWrapper.offsetTop;
        $('html,body').animate({ scrollTop: targetOffset }, 500);
    }
    else 
        errorWrapper.style.display = "none";
}
